<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Buku extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('Buku_model');
		$this->load->helper('all_helper');
		if(!$this->session->userdata('rule')== '1' && !$this->session->userdata('username') ){
			redirect('kategori');
		}
		
		
	}

	function index() {
		$offset = $this->uri->segment(3);
		$config['base_url'] = $this->config->site_url().'buku/index';
		$config['total_rows'] = $this->db->count_all('tb_buku');
		$config['per_page'] = 10;
		$config['uri_segment'] = '3';
			$data['urutan'] = $this->uri->segment(3);
			$data['title'] = 'Data Buku';
			$data['status'] = 'Data Buku';
			$data['main_view'] = 'buku/buku';
			$data['query'] = $this->Buku_model->getBuku('list',FALSE,$config['per_page'],$offset);
				$this->pagination->initialize($config);
				$this->load->view('index',$data);
	
	
	}
	function cari(){
		$offset = $this->uri->segment(3);
		$config['base_url'] = $this->config->site_url().'buku/index';
		$config['total_rows'] = $this->db->count_all('tb_buku');
		$config['per_page'] = 10;
		$config['uri_segment'] = '3';
			$data['urutan'] = $this->uri->segment(3);
			$data['title'] = 'Data Buku';
			$data['status'] = 'Cari Data Buku';
			$data['main_view'] = 'buku/buku';
				$cari = $this->input->post('search');
			$data['query'] = $this->Buku_model->getBuku('cari',FALSE,$cari,$config['per_page'],$offset);
				$this->pagination->initialize($config);
				$this->load->view('index',$data);
	
	
	}
	
	function add(){
		$this->form_validation->set_rules('kode', 'kode', 'required');
		$this->form_validation->set_rules('judul', 'judul', 'required');
		$this->form_validation->set_rules('pengarang', 'pengarang', 'required');
		$this->form_validation->set_rules('nomer', 'nomer', 'required');
		$this->form_validation->set_rules('no_rak', 'no_rak', 'required');
			if($this->form_validation->run() == FALSE) {
					$data['title']= 'Tambah Data Buku';
					$data['status'] = 'Tambah Data Buku';
					$data['main_view']= 'buku/add';
					$this->load->view('index',$data);
			}
			else{
			$data = array('kode_buku'	=> $this->input->post('kode'),
						'judul'			=> $this->input->post('judul'),
						'pengarang'		=> $this->input->post('pengarang'),
						'no_buku'		=> $this->input->post('nomer'),
						'id_kategori'	=> $this->input->post('id_kategori'),
						'id_penerbit'	=> $this->input->post('id_penerbit'),
						'kode_rak'		=> $this->input->post('no_rak'));
						$this->Buku_model->addBuku($data);
						redirect('buku');
						
			
			
			}
	
	
	}
	function edit(){
	
			
		$this->form_validation->set_rules('kode', 'kode', 'required');
			$this->form_validation->set_rules('judul', 'judul', 'required');
			$this->form_validation->set_rules('pengarang', 'pengarang', 'required');
			$this->form_validation->set_rules('nomer', 'nomer', 'required');
			$this->form_validation->set_rules('no_rak', 'no_rak', 'required');
				if($this->form_validation->run() == FALSE) {
						$id_buku = $this->uri->segment(3);
						$data['title']= 'Tambah Data Buku';
						$data['status'] = 'Edit Data Buku';
						$data['main_view']= 'buku/edit';
						$data['row'] = $this->Buku_model->getBuku('by_id',$id_buku,FALSE,FALSE);
						$this->load->view('index',$data);
				}
				else{
				$id= $this->uri->segment(3);
				$data = array('kode_buku'	=> $this->input->post('kode'),
							'judul'			=> $this->input->post('judul'),
							'pengarang'		=> $this->input->post('pengarang'),
							'no_buku'		=> $this->input->post('nomer'),
							'id_kategori'	=> $this->input->post('id_kategori'),
							'id_penerbit'	=> $this->input->post('id_penerbit'),
							'kode_rak'		=> $this->input->post('no_rak'));
							$this->Buku_model->editBuku($id,$data);
							redirect('buku');	
		
		
		}
		
	}
	
	function delete(){
		$id = $this->uri->segment(3);
		$this->Buku_model->deleteBuku($id);
		redirect('buku');
	
	}
	
	
}